/*
 * Copyright (c) 2024-present HBIS Digital Technology Co.,Ltd. All rights reserved.
 */
package com.hbisdt.cet.sample.service.impl;

import com.hbisdt.cet.sample.entity.ResponseData;
import com.hbisdt.cet.sample.entity.dto.LoginRequest;
import com.hbisdt.cet.sample.service.ILoginService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * 登录服务层实现
 *
 * @author : 教学示例
 * @version : 1.0
 * @date : 2025-01-06 14:29
 */
@Service
public class LoginServiceImpl implements ILoginService {

    /**
     * 登录方法
     *
     * @param loginRequest 请求实体
     * @return void
     * @author : 教学示例
     */
    @Override
    public ResponseData<?> login(LoginRequest loginRequest) {
        // 模拟账号密码正确
        if ("admin".equals(loginRequest.getUsername()) && "123456".equals(loginRequest.getPassword())) {
            // 用户信息保存到session
            HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
            request.getSession().setAttribute("loginUserName", loginRequest.getUsername());
            return ResponseData.ok();
        } else {
            return ResponseData.failed("用户名或密码错误");
        }
    }

}
